package laugh.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import laugh.dao.ContentDao;
import laugh.pojo.Content;
import laugh.pojo.ExtendedContent;
import laugh.pojo.OptResultInfo;
import laugh.service.ContentService;

import org.apache.commons.beanutils.BeanUtils;

import com.google.gson.Gson;

/**
 * 
*    
* 项目名称：LaughFactory   
* 类名称：ContentServlet   
* 类描述：  内容服务Servlet
* 创建人：wizard   
* 创建时间：2012-3-30 下午03:15:59   
* 修改人：wizard   
* 修改时间：2012-3-30 下午03:15:59   
* 修改备注：   
* @version 0.9
*
 */
public class ContentServlet extends HttpServlet {

    private static final long serialVersionUID = 8900958982667572007L;

    public ContentServlet() {
        super();
    }

    public void destroy() {
        super.destroy();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(); 
//        Long uid=(Long)session.getAttribute("uid");
        String service=request.getParameter("service");
        if("addContent".equals(service)){//添加新内容
            Content content=new Content();
            try {
                BeanUtils.populate(content, request.getParameterMap());
                OptResultInfo optResultInfo=new ContentService().addContentWithCheck(content, session);
                out.print(new Gson().toJson(optResultInfo).toString());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }else if("addContentFromSession".equals(service)){//从session中添加新内容
            OptResultInfo optResultInfo=new ContentService().addContentFromSession(session);
            out.print(new Gson().toJson(optResultInfo).toString());
        }else if("deleteContent".equals(service)){//删除一个内容
            OptResultInfo optResultInfo=new OptResultInfo();
            String idString=request.getParameter("id");
            if(idString==null||"".equals(idString)){
                optResultInfo.setCode(0);
                optResultInfo.setInfo(1L);
                optResultInfo.setMeg("id为空");
            }else{
                optResultInfo=new ContentDao().deleteContent(Long.parseLong(idString)); 
            }
            out.print(new Gson().toJson(optResultInfo).toString());
        }else if("updateContent".equals(service)){//打算暂时不支持更新
            
        }else if("loadContent".equals(service)){//加载一篇内容
            String idString=request.getParameter("id");
            Content content;
            if(idString==null||"".equals(idString)){
                content=new Content();
            }else{
                content=new ContentDao().loadContent(Long.parseLong(idString));  
            }
            out.print(new Gson().toJson(content).toString());
        }else if("loadLastestContent".equals(service)){
            Long min_id=Long.parseLong(request.getParameter("min_id"));
            List<ExtendedContent> result = new ContentDao().loadLastestContent(1L,min_id);
            out.print(new Gson().toJson(result).toString()); 
        }else if("loadSpecialContent".equals(service)){
            Long max_id=Long.parseLong(request.getParameter("max_id"));
            List<ExtendedContent> result = new ContentDao().loadSpecialContent(1L,max_id);
            out.print(new Gson().toJson(result).toString());
        }else if("loadHotestContent".equals(service)){
            
        }else{
            OptResultInfo optResultInfo=new OptResultInfo();
            optResultInfo.setCode(0);
            optResultInfo.setInfo(1L);
            optResultInfo.setMeg("未找到对应的服务");
            out.print(new Gson().toJson(optResultInfo).toString());
        }
        out.flush();
        out.close();
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

    public void init() throws ServletException {
        
    }

}
